package com.lisen.codegenerator.util;

public class GeneratorUtility {
	private static GeneratorUtility instance;

	// private GeneratorUtility() {
	// }

	public static GeneratorUtility getInstance() {
		if (null == instance) {
			instance = new GeneratorUtility();
		}
		return instance;
	}

	public static boolean isEmpty(Object obj) {
		if (obj == null) {
			return true;
		} else if (obj instanceof String) {
			if ("".equals(((String) obj).trim())) {
				return true;
			}
		}
		return false;
	}

	public static String firstToUpperCase(String string) {
		if (!isEmpty(string)) {
			String post = string.substring(1, string.length());
			String first = ("" + string.charAt(0)).toUpperCase();
			return first + post;
		} else {
			return string;
		}
	}

	public static String upperCase(String str) {
		if (!isEmpty(str)) {
			return str.toUpperCase();
		}
		return str;
	}

	public static String alt(String str) {
		String result = "";
		if (str.equalsIgnoreCase("true")) {
			result = "alt=\"allowBlank:false,vtext:'请输入必填项;最多输入128字符',maxLength:128\"";
		}
		return result;
	}

	/**
	 * 
	 * @param javaType
	 *            java类型
	 * @param dbType
	 *            数据库类型
	 * @return
	 */
	public static String getSqlType(String javaType, int columnSize,
			String dbType) {
		String rv = "";
		if ("oracle".equalsIgnoreCase(dbType)) {
			if ("string".equalsIgnoreCase(javaType)) {
				rv = "varchar2(" + columnSize + ")";
			} else if ("number".equalsIgnoreCase(javaType)) {
				rv = "number";
			} else if ("timestamp".equalsIgnoreCase(javaType)) {
				rv = "date";
			}
		}
		return rv;
	}

	public static String getFormBeanType(String sqlType) {
		String rv = "";
		rv = "String";
		return rv;
	}

	public static String nullObject2String(Object s) {
		String str = "";
		try {
			str = s.toString();
		} catch (Exception e) {
			str = "";
		}
		return str;
	}

	public static String nullObject2String(Object s, String chr) {
		String str = chr;
		try {
			str = s.toString();
		} catch (Exception e) {
			str = chr;
		}
		return str;
	}

	public static int nullObject2int(Object s) {
		String str = "";
		int i = 0;
		try {
			str = s.toString();
			i = Integer.parseInt(str);
		} catch (Exception e) {
			i = 0;
		}
		return i;
	}

	/**
	 * 
	 * @param sqlType
	 * @return
	 */
	public static String getJavaPOType(String sqlType) {
		String rv = null;
		if ("string".equalsIgnoreCase(sqlType)) {
			rv = "String";
		} else if ("float".equalsIgnoreCase(sqlType)) {
			rv = "double";
		} else if ("double".equalsIgnoreCase(sqlType)) {
			rv = "double";
		} else if ("int".equalsIgnoreCase(sqlType)) {
			rv = "long";
		}

		return rv;
	}

	public static String getJavaVOType(String sqlType) {
		String rv = "";
		if ("string".equalsIgnoreCase(sqlType)) {
			rv = "String";
		} else if ("float".equalsIgnoreCase(sqlType)) {
			rv = "double";
		} else if ("integer".equalsIgnoreCase(sqlType)) {
			rv = "int";
		} else if ("double".equalsIgnoreCase(sqlType)) {
			rv = "double";
		} else if ("date".equalsIgnoreCase(sqlType)) {
			rv = "String";
		} else if ("timestamp".equalsIgnoreCase(sqlType)) {
			rv = "String";
		} else {
		}

		return rv;
	}
}
